package lesson8.chat.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.util.Objects;

/**
 * 聊天室服务端<br/>
 * Created on : 2018-01-29 23:10
 * @author lizebin
 */
public class Server {

	public static void main(String[] args) {
		//接收连接，交给worker线程组处理
		EventLoopGroup bossGroup = new NioEventLoopGroup(1);
		//真正处理的请求的线程组
		EventLoopGroup workerGroup = new NioEventLoopGroup(1);
		try {
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ServerInitializer());
			ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
			channelFuture.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			if(Objects.nonNull(bossGroup)) {
				bossGroup.shutdownGracefully();
			}
			if(Objects.nonNull(workerGroup)) {
				workerGroup.shutdownGracefully();
			}
		}

	}

}
